home *** CD-ROM | disk | FTP | other *** search
/ Die Speccy' 97 / Die Speccy' 97.iso / amiga_system / the_aminet / util / misc / wgrab.lha / wshot.c < prev    next >
C/C++ Source or Header  |  1995-10-30  |  7KB  |  181 lines

  1. /* $Id: wshot.c,v 1.2 1995/10/29 09:41:56 aris Exp aris $
  2. */
  3. #include <intuition/IntuitionBase.h>
  4. #include <graphics/gfxbase.h>
  5. #include <datatypes/pictureclass.h>
  6. #include <exec/memory.h>
  7. #include <dos/dos.h>
  8.  
  9. #include <clib/datatypes_protos.h>
  10. #include <clib/intuition_protos.h>
  11. #include <clib/graphics_protos.h>
  12. #include <clib/alib_protos.h>
  13. #include <clib/exec_protos.h>
  14. #include <clib/dos_protos.h>
  15. #include <clib/macros.h>
  16.  
  17. extern struct IntuitionBase *IntuitionBase;
  18. extern struct Library *DataTypesBase;
  19.  
  20. char *MYLIBNAME="datatypes.library";
  21.  
  22. VOID
  23. SavePicture(Object *Picture,const STRPTR FileName)
  24. {
  25.         BPTR FileHandle;
  26.  
  27.         if(FileHandle = Open(FileName,MODE_NEWFILE))
  28.         {
  29.                 if(DoMethod(Picture,DTM_WRITE,NULL,FileHandle,DTWM_IFF,NULL))
  30.                         Close(FileHandle);
  31.                 else
  32.                 {
  33.                         Close(FileHandle);
  34.  
  35.                         DeleteFile(FileName);
  36.                 }
  37.         }
  38.  
  39.         DisposeDTObject(Picture);
  40. }
  41.  
  42. Object *
  43. GetPicture(BOOL scr)
  44. {
  45.         struct RastPort *RPort;
  46.  
  47.         if(RPort = (struct RastPort *)AllocVec(sizeof(struct RastPort),MEMF_ANY))
  48.         {
  49.                 struct BitMap   *BitMap;
  50.                 ULONG                    IntuiLock;
  51.                 struct Window   *Window;
  52.                 LONG                     Left,Top,
  53.                                          Width,Height,
  54.                                          PageWidth,PageHeight,
  55.                                          Depth;
  56.                 BOOL                     Locked = TRUE;
  57.                 struct Screen *Screen;
  58.  
  59.                 InitRastPort(RPort);
  60.  
  61.                 IntuiLock = LockIBase(NULL);
  62.  
  63.                 if(!scr) {
  64.  
  65.                     Window          = IntuitionBase -> ActiveWindow;
  66.  
  67.                     Left            = Window -> LeftEdge;
  68.                     Top             = Window -> TopEdge;
  69.                     Width           = Window -> Width;
  70.                     Height          = Window -> Height;
  71.                     PageWidth       = Window -> WScreen -> Width;
  72.                     PageHeight      = Window -> WScreen -> Height;
  73.                     Depth           = GetBitMapAttr(Window -> RPort -> BitMap,BMA_DEPTH);
  74.                     }
  75.                 else {
  76.  
  77.                     Screen          = IntuitionBase -> ActiveScreen;
  78.  
  79.                     Left            = Screen -> LeftEdge;
  80.                     Top             = Screen -> TopEdge ;
  81.                     Width           = Screen -> Width;
  82.                     Height          = Screen -> Height;
  83.                     PageWidth       = Width;
  84.                     PageHeight      = Height;
  85.                     Depth           = GetBitMapAttr(Screen -> RastPort . BitMap,BMA_DEPTH);
  86.                     }
  87.  
  88.                 if(BitMap = AllocBitMap(Width,Height,Depth,BMF_CLEAR,
  89.                         ((scr)?(Screen -> RastPort . BitMap):(Window -> RPort -> BitMap))))
  90.                 {
  91.                         ULONG   *ColourTable,
  92.                                          ModeID;
  93.                         LONG     NumColours;
  94.  
  95.                         NumColours = ((scr)?(Screen -> ViewPort . ColorMap -> Count):(Window -> WScreen -> ViewPort . ColorMap -> Count));
  96.  
  97.                         ModeID = GetVPModeID(((scr)?(&Screen -> ViewPort):(&Window -> WScreen -> ViewPort)));
  98.  
  99.                         RPort -> BitMap = BitMap;
  100.  
  101.                         ClipBlit(((scr)?(&Screen -> RastPort):(Window -> RPort)),0,0,RPort,0,0,Width,Height,0xC0);
  102.  
  103.                         WaitBlit();
  104.  
  105.                         if(ColourTable = (ULONG *)AllocVec(sizeof(ULONG) * 3 * NumColours,
  106.                                 MEMF_ANY))
  107.                         {
  108.                                 Object *Picture;
  109.  
  110.                                 GetRGB32(((scr)?(Screen -> ViewPort . ColorMap):(Window -> WScreen -> ViewPort . ColorMap)),0,NumColours,
  111.                                         ColourTable);
  112.  
  113.                                 UnlockIBase(IntuiLock);
  114.  
  115.                                 Locked = FALSE;
  116.  
  117.                                 if(Picture = NewDTObject("ActiveWindow",
  118.                                         DTA_SourceType, DTST_RAM,
  119.                                         DTA_GroupID,    GID_PICTURE,
  120.                                         PDTA_NumColors, NumColours,
  121.                                         PDTA_BitMap,    BitMap,
  122.                                         PDTA_ModeID,    ModeID,
  123.                                 TAG_DONE))
  124.                                 {
  125.                                         struct ColorRegister    *ColourMap;
  126.                                         struct BitMapHeader             *BitMapHeader;
  127.                                         ULONG                                   *Colours;
  128.  
  129.                                         if(GetDTAttrs(Picture,
  130.                                                 PDTA_BitMapHeader,      &BitMapHeader,
  131.                                                 PDTA_ColorRegisters,&ColourMap,
  132.                                                 PDTA_CRegs,                     &Colours,
  133.                                         TAG_DONE) == 3)
  134.                                         {
  135.                                                 BitMapHeader -> bmh_Left                = Left;
  136.                                                 BitMapHeader -> bmh_Top                 = Top;
  137.                                                 BitMapHeader -> bmh_Width               = Width;
  138.                                                 BitMapHeader -> bmh_Height              = Height;
  139.                                                 BitMapHeader -> bmh_Depth               = Depth;
  140.                                                 BitMapHeader -> bmh_PageWidth           = PageWidth;
  141.                                                 BitMapHeader -> bmh_PageHeight          = PageHeight;
  142.  
  143.                                                 CopyMem(ColourTable,Colours,
  144.                                                         3 * sizeof(ULONG) * NumColours);
  145.  
  146.                                                 while(NumColours--)
  147.                                                 {
  148.                                                         ColourMap -> red        = (UBYTE)((*Colours++) >> 24);
  149.                                                         ColourMap -> green      = (UBYTE)((*Colours++) >> 24);
  150.                                                         ColourMap -> blue       = (UBYTE)((*Colours++) >> 24);
  151.  
  152.                                                         ColourMap++;
  153.                                                 }
  154.  
  155.                                                 FreeVec(ColourTable);
  156.  
  157.                                                 FreeVec(RPort);
  158.  
  159.                                                 return(Picture);
  160.                                         }
  161.  
  162.                                         DisposeDTObject(Picture);
  163.  
  164.                                         BitMap = NULL;
  165.                                 }
  166.  
  167.                                 FreeVec(ColourTable);
  168.                         }
  169.  
  170.                         FreeBitMap(BitMap);
  171.                 }
  172.  
  173.                 if(Locked)
  174.                         UnlockIBase(IntuiLock);
  175.  
  176.                 FreeVec(RPort);
  177.         }
  178.  
  179.         return(NULL);
  180. }
  181.